import Vue from 'vue';
import VueRouter from 'vue-router';
import routerChilder from './routerChilder.js'
import HomeView from '@/views/home/Home.vue';
import Login from '@/views/login/Login.vue';
import NoPage from '@/views/404.vue';
Vue.use(VueRouter);
const router = new VueRouter({
  mode: 'hash',
  base: (process.env.NODE_ENV === 'development'?'/':'/admin'),
  routes: [
    {
      path: '/',
      name: 'home',
      meta: { title: '首页' },
      component: HomeView,
      children:routerChilder,
      redirect:'/zhactive'
    },
    {
      path: '/login',
      name: 'login',
      meta: { title: '登录' },
      component: Login,
    },
    {
      path: '*',
      redirect: '/404',
      component: NoPage,
    }
  ],
});

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}



//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
  document.title = `${to.meta.title}`;
  //const role = getCookie('userName');
  const role =window.sessionStorage.getItem('userName');
  if (!role && to.path !== '/login') {
      next('/login');
  } else if (to.meta.permission) {
      // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
      //role === 'admin' ? next() : next('/403');
      next();
  } else {
      // 简单的判断IE10及以下不进入富文本编辑器，该组件不兼容
      if (navigator.userAgent.indexOf('MSIE') > -1 && to.path === '/editor') {
          Vue.prototype.$alert('vue-quill-editor组件不兼容IE10及以下浏览器，请使用更高版本的浏览器查看', '浏览器不兼容通知', {
              confirmButtonText: '确定'
          });
      } else {
          next();
      }
  }
});


export default router;
